#include <stdio.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>


#include "employee_client.h"
#define errlog(errmsg) do{\
							perror(errmsg);\
							printf("%s----%s----%d\n",__FILE__,__func__,__LINE__);\
							exit(1);\
						 }while(0)

//客户端前台界面function

void frame_func()
{
	printf("******************************************\n");
	printf("*                                        *\n");
	printf("*                                        *\n");
	printf("*     登   录    *****    注    册       *\n");
	printf("*                                        *\n");
	printf("*   登录请输入1          注册请输入2     *\n");
	printf("*                                        *\n");
	printf("******************************************\n");
	
	return ;
}

//员工注册模块
int employee_register()
{
	bzero(&client_block,sizeof(client_block));
	printf("***************员工正在进行注册***************\n");
	printf("please input name:");
	scanf("%s",client_block.name);
	printf("please input passwd:");
	scanf("%s",client_block.passwd);

	if((send(sockfd,&client_block,sizeof(client_block),0)) < 0){
		errlog("fail to send......");
	}

	bzero(&client_block,sizeof(client_block));
	return 0;
}

//员工登录模块
int employee_log_in()
{

	bzero(&client_block,sizeof(client_block)); // 清空传输结构体
	printf("***************员工正在进行登录***************\n");
	printf("please input name:");
	scanf("%s",client_block.name);
	printf("please input passwd:");
	scanf("%d",client_block.passwd);

	if((send(sockfd,&client_block,sizeof(client_block),0)) < 0){
		errlog("fail to send......");
	}

	bzero(&client_block,sizeof(client_block));

	if((recv(sockfd,&client_block,sizeof(client_block),0)) < 0){
		errlog("log_in fail to recv......");
	}

	printf("%s\n",client.e_msg.msg);//接收服务器端的登录成功信息
	
	printf("***************************************************\n");
	printf("*            (A)：修改个人信息  				  *\n");
	printf("*   											  *\n");
	printf("*            (B):   上传文件					  *\n");
	printf("*   											  *\n");
	printf("*            (C)：  下载文件  		       		  *\n");
	printf("*                  					       		  *\n");
	printf("*            (D):  查看个人信息     	   		  *\n");
	printf("*                  					       		  *\n");
	printf("*            (E)：    退出 			       		  *\n");
	printf("***************************************************\n");
	
	while(1){

		bzero(&client_block,sizeof(client_block));
		printf("please input your option:");
		scanf("%c",&client_block.func_type);
		switch(client_block.func_type)
		{
			case A:
				alter_person_msg();//修改个人信息
				break;
			case B:
				upload_file();//上传本地文件
				break;
			case C:
				download_file();//下载服务器端文件
				break;
			case D:
				employee_lookup_person_msg();//员工查看本人信息
				break;
			case E:
				return 0;

		}

	}


	return 0;
}
// 管理员没登录模块
int admin_log_in()
{

	bzero(&client_block,sizeof(client_block)); // 清空传输结构体
	printf("**************管理员正在进行登录***************\n");
	printf("please input name:");
	scanf("%s",client_block.name);
	printf("please input passwd:");
	scanf("%d",client_block.passwd);

	if((send(sockfd,&client_block,sizeof(client_block),0)) < 0){
		errlog("fail to send......");
	}

	bzero(&client_block,sizeof(client_block));

	if((recv(sockfd,&client_block,sizeof(client_block),0)) < 0){
		errlog("log_in fail to recv......");
	}

	printf("%s\n",client.e_msg.msg);//接收服务器端的登录成功信息
	
	printf("***************************************************\n");
	printf("*            (A)：     修改个人信息  		  	  *\n");
	printf("*   											  *\n");
	printf("*            (B):        上传文件				  *\n");
	printf("*   											  *\n");
	printf("*            (C)：       下载文件  		       	  *\n");
	printf("*                  					       		  *\n");
	printf("*            (F):  查看所有员工个人信息     	  *\n");
	printf("*                  					       		  *\n");
	printf("*            (G):        注册员工  				  *\n");
	printf("*                  					       		  *\n");
	printf("*            (H):        删除员工    			  *\n");
	printf("*                  					       		  *\n");
	printf("*            (E)：         退出 			      *\n");
	printf("***************************************************\n");
	
	while(1){

		bzero(&client_block,sizeof(client_block));
		printf("please input your option:");
		scanf("%c",&client_block.func_type);
		switch(client_block.func_type)
		{
			case A:
				alter_person_msg();//修改个人信息
				break;
			case B:
				upload_file();//上传本地文件
				break;
			case C:
				download_file();//下载服务器端文件
				break;
			case F:
				admin_lookup_employ_msg();//管理员查看员工信息
				break;
			case G:
				admin_register_employee();//管理员注册员工信息
				break;
			case H:
				admin_delete_employee();//管理员删除员工信息
				break;
			case E:
				return 0;

		}

	}
	return 0;
}
//修改个人信息模块
int alter_person_msg()
{

	//修改密码
	printf("please input your new passwd ");
	scanf("%s",client_block.passwd);
	
	if((send(sockfd,&client_block,sizeof(client_block),0)) < 0){
		errlog("fail to send......");
	}

	bzero(&client_block,sizeof(client_block));

	if((recv(sockfd,&client_block,sizeof(client_block),0)) < 0){
		errlog("log_in fail to recv......");
	}

	printf("%s\n",client.e_msg.msg);//接收服务器端修改信息是否成功的信息
	
	return 0;

}

//上传本地文件

void upload_file()
{ 
	int file_fd,ret;
	printf("请输入本地文件名：");
	scanf("%s",client_block.file_name);

	file_fd = open(client_block.file_name,O_RDONLY);
	if(file_fd < 0){
		errlog("fail to open (file_fd)"); //打开文件失败
	}
	
	//发送上传文件通知
	if((send(sockfd,&client_block,sizeof(client_block),0)) < 0){
		errlog("fail to send......");
	}
	
	//接收可以上传文件通知
	if((recv(sockfd,&client_block,sizeof(client_block),0)) < 0){
		errlog("log_in fail to recv......");
	}
	

	while(1){

		bzero(client_block.e_msg.msg,sizeof(client_block.e_msg.msg));//每次清空缓冲去消息

		ret = read(file_fd,client_block.e_msg.msg,sizeof(client_block.e_msg.msg));
		if(ret < 0){
			
			break;
		}else if(ret == 0){
			printf("文件已上传完成 ......");
			break;
		}

		//发送读取到的文件信息
		if((send(sockfd,&client_block,sizeof(client_block),0)) < 0){
			errlog("fail to send......");
		}

		sleep(1);
	}
	close(file_fd);
	return ;
}

//下载服务器端文件

void download_file()
{
	int file_fd,ret;
	printf("请输入要下载的文件名字：");
	scanf("%s",&client_block.file_name);
	
	file_fd = open(client_block.file_name,O_WRONLY|O_CREAT,0666);
	if(file_fd < 0){
		errlog("fail to open (file_fd)");	//打开文件失败
	}


	//发送下载文件通知
	if((send(sockfd,&client_block,sizeof(client_block),0)) < 0){
		errlog("fail to send......");
	}
	
	//接收可以下载文件通知
	if((recv(sockfd,&client_block,sizeof(client_block),0)) < 0){
		errlog("log_in fail to recv......");
	}
	
	while(1){
		
		bzero(client_block.e_msg.msg,sizeof(client_block.e_msg.msg));//每次清空缓冲去消息

		if((recv(sockfd,&client_block,sizeof(client_block),0)) < 0){
			errlog("log_in fail to recv......");
		}
			
		sleep(1);

		ret = write(file_fd,client_block.e_msg.msg,sizeof(client_block.e_msg.msg));
		if(ret < 0){
			break;
		}

	}
	close(file_fd);
	return ;
}

//员工查看本人信息
 
void employee_lookup_person_msg()
{
	//发送查看本人信息的通知
	if((send(sockfd,&client_block,sizeof(client_block),0)) < 0){
		errlog("fail to send......");
	}
	
	//接收查看本人信息成功的通知
	if((recv(sockfd,&client_block,sizeof(client_block),0)) < 0){
		errlog("log_in fail to recv......");
	}
	printf("员工姓名：%s\n",client_block.name);

	return ;

}

//管理员查看员工信息

void admin_lookup_employ_msg()
{
	//管理员发送查看那员工信息的通知
	if((send(sockfd,&client_block,sizeof(client_block),0)) < 0){
		errlog("fail to send......");
	}
	
	while(1){
		bzero(client_block.name,sizeof(client_block.name));
		//接受返回的员工名单
		if((recv(sockfd,&client_block,sizeof(client_block),0)) < 0){
			errlog("log_in fail to recv......");
		}
		
		if(strcmp(client_block.name,"aaa"){
			break;		
		}

		printf("员工姓名：%s\n",client_block.name);
	}

}


//管理员注册员工信息

void admin_register_employee()
{
	printf("************管理员注册员工信息***************\n");
	printf("please input name:");
	scanf("%s",client_block.name);
	printf("please input passwd:");
	scanf("%s",client_block.passwd);
	
	//管理员发送注册员工信息的通知
	if((send(sockfd,&client_block,sizeof(client_block),0)) < 0){
		errlog("fail to send......");
	}
	
	//接收注册员工信息成功的通知
	if((recv(sockfd,&client_block,sizeof(client_block),0)) < 0){
		errlog("log_in fail to recv......");
	}

	printf("%s\n",client_block.e_msg.msg);//打印注册员工信息成功字样

	return ;

}

//管理员删除员工信息
void admin_delete_employee()
{
	
	printf("************管理员删除员工信息***************\n");
	printf("please input name:");
	scanf("%s",client_block.name);

	//管理员发送删除员工信息的通知
	if((send(sockfd,&client_block,sizeof(client_block),0)) < 0){
		errlog("fail to send......");
	}
	
	//接收删除员工信息成功的通知
	if((recv(sockfd,&client_block,sizeof(client_block),0)) < 0){
		errlog("log_in fail to recv......");
	}
	
	printf("%s\n",client_block.e_msg.msg);//打印删除员工成功字样

	return ;

}




